home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / Audio.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  5.3 KB  |  209 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef f_AUDIO_H
  19. #define f_AUDIO_H
  20.  
  21. #include <windows.h>
  22. #include <mmsystem.h>
  23. #include <msacm.h>
  24.  
  25. #include "FrameSubset.h"
  26.  
  27. typedef void (*AudioFormatConverter)(void *, void *, long);
  28. typedef long (*AudioPointSampler)(void *, void *, long, long, long);
  29. typedef long (*AudioUpSampler)(void *, void *, long, long, long);
  30. typedef long (*AudioDownSampler)(void *, void *, long *, int, long, long, long);
  31.  
  32. class AudioSource;
  33.  
  34. ///////
  35.  
  36. class AudioStream {
  37. protected:
  38.     WAVEFORMATEX *format;
  39.     long format_len;
  40.  
  41.     AudioStream *source;
  42.     long samples_read;
  43.     long stream_len;
  44.     long stream_limit;
  45.  
  46.     AudioStream();
  47.  
  48.     WAVEFORMATEX *AllocFormat(long len);
  49. public:
  50.     virtual ~AudioStream();
  51.  
  52.     virtual WAVEFORMATEX *GetFormat();
  53.     virtual long GetFormatLen();
  54.     virtual long GetSampleCount();
  55.     virtual long GetLength();
  56.  
  57.     virtual long _Read(void *buffer, long max_samples, long *lplBytes);
  58.     virtual long Read(void *buffer, long max_samples, long *lplBytes);
  59.     virtual bool Skip(long samples);
  60.     virtual void SetSource(AudioStream *source);
  61.     virtual void SetLimit(long limit);
  62.     virtual BOOL isEnd();
  63.     virtual BOOL _isEnd();
  64. };
  65.  
  66. class AudioStreamSource : public AudioStream {
  67. private:
  68.     AudioSource *aSrc;
  69.     WAVEFORMATEX *pwfexTempInput;
  70.     long cur_samp;
  71.     long end_samp;
  72.     HACMSTREAM hACStream;
  73.     ACMSTREAMHEADER ashBuffer;
  74.     void *inputBuffer;
  75.     void *outputBuffer;
  76.     char *outputBufferPtr;
  77.     long lPreskip;
  78.     bool fZeroRead;
  79.     bool fStart;
  80.  
  81.     enum { INPUT_BUFFER_SIZE = 16384 };
  82.  
  83. public:
  84.     AudioStreamSource(AudioSource *src, long first_sample, long max_sample, BOOL allow_decompression);
  85.     ~AudioStreamSource();
  86.  
  87.     long _Read(void *buffer, long max_samples, long *lplBytes);
  88.     bool Skip(long samples);
  89.     BOOL _isEnd();
  90. };
  91.  
  92. class AudioStreamConverter : public AudioStream {
  93. private:
  94.     AudioFormatConverter convRout;
  95.     void *cbuffer;
  96.     int bytesPerInputSample, bytesPerOutputSample;
  97.     int offset;
  98.  
  99.     enum { BUFFER_SIZE=4096 };
  100.  
  101. public:
  102.     AudioStreamConverter(AudioStream *src, bool to_16bit, bool to_stereo_or_right, bool single_only);
  103.     ~AudioStreamConverter();
  104.  
  105.     long _Read(void *buffer, long max_samples, long *lplBytes);
  106.     BOOL _isEnd();
  107. };
  108.  
  109. class AudioStreamResampler : public AudioStream {
  110. private:
  111.     AudioPointSampler ptsampleRout;
  112.     AudioUpSampler upsampleRout;
  113.     AudioDownSampler dnsampleRout;
  114.     void *cbuffer;
  115.     int bytesPerSample;
  116.     long samp_frac;
  117.     long accum;
  118.     int holdover;
  119.     long *filter_bank;
  120.     int filter_width;
  121.     bool fHighQuality;
  122.  
  123.     enum { BUFFER_SIZE=512 };
  124.  
  125.     long Upsample(void *buffer, long samples, long *lplBytes);
  126.     long Downsample(void *buffer, long samples, long *lplBytes);
  127.  
  128. public:
  129.     AudioStreamResampler(AudioStream *source, long new_rate, bool integral_rate, bool high_quality);
  130.     ~AudioStreamResampler();
  131.  
  132.     long _Read(void *buffer, long max_samples, long *lplBytes);
  133.     BOOL _isEnd();
  134. };
  135.  
  136. class AudioCompressor : public AudioStream {
  137. private:
  138.     HACMSTREAM hACStream;
  139.     HACMDRIVER hADriver;
  140.     ACMSTREAMHEADER ashBuffer;
  141.     WAVEFORMATEX *pwfexTempOutput;
  142.     void *inputBuffer;
  143.     void *outputBuffer;
  144.     char *outputBufferPtr;
  145.     void *holdBuffer;
  146.     long holdBufferSize;
  147.     long holdBufferOffset;
  148.     BOOL fStreamEnded;
  149.     LONG bytesPerInputSample;
  150.     LONG bytesPerOutputSample;
  151.  
  152.     enum { INPUT_BUFFER_SIZE = 16384 };
  153.  
  154.     void    ResizeHoldBuffer(long lNewSize);
  155.     void    WriteToHoldBuffer(void *data, long lBytes);
  156.  
  157. public:
  158.     AudioCompressor(AudioStream *src, WAVEFORMATEX *dst_format, long dst_format_len);
  159.     ~AudioCompressor();
  160.     void CompensateForMP3();
  161.     void *    Compress(long lInputSamples, long *lplSrcInputSamples, long *lplOutputBytes, long *lplOutputSamples);
  162.     BOOL    isEnd();
  163. };
  164.  
  165. class AudioL3Corrector {
  166. private:
  167.     long samples, frame_bytes, read_left;
  168.     bool header_mode;
  169.     char hdr_buffer[4];
  170.  
  171. public:
  172.     AudioL3Corrector();
  173.     long ComputeByterate(long sample_rate);
  174.     void Process(void *buffer, long bytes);
  175. };
  176.  
  177. class AudioSubset : public AudioStream {
  178. private:
  179.     FrameSubset subset;
  180.     FrameSubsetNode *pfsnCur;
  181.     int iOffset;
  182.     long lSrcPos;
  183.     long lSkipSize;
  184.  
  185.     char skipBuffer[512];
  186.  
  187. public:
  188.     AudioSubset(AudioStream *, FrameSubset *, long, long);
  189.     ~AudioSubset();
  190.     long _Read(void *, long, long *);
  191.     BOOL _isEnd();
  192. };
  193.  
  194. class AudioStreamAmplifier : public AudioStream {
  195. private:
  196.     long lFactor;
  197.  
  198. public:
  199.     AudioStreamAmplifier(AudioStream *src, long lFactor);
  200.     ~AudioStreamAmplifier();
  201.  
  202.     long _Read(void *buffer, long max_samples, long *lplBytes);
  203.     BOOL _isEnd();
  204. };
  205.  
  206. long AudioTranslateVideoSubset(FrameSubset& dst, FrameSubset& src, long usPerFrame, WAVEFORMATEX *pwfex);
  207.  
  208. #endif
  209.